import 'dart:convert';

import 'package:ichinesereader/models/book/BookLevel.dart';
import 'package:ichinesereader/models/common/Settings.dart';

List<StudentItem> studentItemsFromJson(String str) => List<StudentItem>.from(
    json.decode(str).map((x) => StudentItem.fromJson(x)));

String studentItemsToJson(List<StudentItem> data) =>
    json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class StudentItem {
  StudentItem({
    this.id,
    this.userId,
    this.userName,
    this.firstName,
    this.lastName,
    this.nickName,
    this.points,
    this.classNames,
    this.playerLevel,
    this.levelPercentage,
    this.accessCode,
    this.gameDisable,
    this.deleted,
    this.disabled,
    this.email,
    this.gender,
    this.dateOfBirth,
    this.parentEmail,
    this.level,
    this.numQuizzes,
    this.numBooks,
    this.lastLoggedIn,
    this.settings,
    this.totalLoginTime,
    this.totalGameTime,
    this.timestampCreated,
    this.excludeFromLeaderboard,
    this.profileImageUrl,
  });

  String? id;
  String? userName;
  String? firstName;
  String? lastName;
  String? nickName;
  int? points;
  dynamic classNames;
  Level? playerLevel;
  int? levelPercentage;
  String? userId;
  String? accessCode;
  bool? gameDisable;
  bool? deleted;
  bool? disabled;
  String? email;
  String? gender;
  String? dateOfBirth;
  String? parentEmail;
  String? level;
  int? numQuizzes;
  int? numBooks;
  int? lastLoggedIn;
  Settings? settings;
  int? totalLoginTime;
  int? totalGameTime;
  int? timestampCreated;
  bool? excludeFromLeaderboard;
  String? profileImageUrl;

  factory StudentItem.fromJson(Map<String, dynamic> json) => StudentItem(
      id: json["id"],
      userName: json["userName"],
      firstName: json["firstName"],
      lastName: json["lastName"],
      nickName: json["nickName"],
      points: json["points"],
      // classNames:  json["classNames"] != null
      //     ? List<String>.from(json["classNames"].map((x) => x))
      //     : [],
      classNames: json["classNames"] != null ? json["classNames"] : null,
      playerLevel: json["playerLevel"] != null
          ? Level.fromJson(json["playerLevel"])
          : null,
      levelPercentage: json["levelPercentage"],
      userId: json["userId"],
      accessCode: json["accessCode"],
      gameDisable: json["gameDisable"],
      deleted: json["deleted"],
      disabled: json["disabled"],
      email: json["email"] == null ? null : json["email"],
      gender: json["gender"] == null ? null : json["gender"],
      dateOfBirth: json["dateOfBirth"] == null ? null : json["dateOfBirth"],
      parentEmail: json["parentEmail"] == null ? null : json["parentEmail"],
      level: json["level"],
      numQuizzes: json["numQuizzes"],
      numBooks: json["numBooks"],
      lastLoggedIn: json["lastLoggedIn"] != null ? json["lastLoggedIn"] : 0,
      settings:
          json["settings"] != null ? Settings.fromJson(json["settings"]) : null,
      totalLoginTime: json["totalLoginTime"],
      totalGameTime: json["totalGameTime"],
      timestampCreated:
          json["timestampCreated"] != null ? json["timestampCreated"] : 0,
      excludeFromLeaderboard: json["excludeFromLeaderboard"],
      profileImageUrl: json["profileImageUrl"]);

  Map<String, dynamic> toJson() => {
        "id": id,
        "userName": userName,
        "firstName": firstName,
        "lastName": lastName,
        "nickName": nickName == null ? null : nickName,
        "points": points,
        "className": classNames,
        // "className": classNames != null
        //     ? List<dynamic>.from(classNames!.map((x) => x))
        //     : null,
        "playerLevel": playerLevel != null ? playerLevel?.toJson() : null,
        "levelPercentage": levelPercentage,
        "userId": userId,
        "accessCode": accessCode,
        "gameDisable": gameDisable,
        "deleted": deleted,
        "disabled": disabled,
        "email": email == null ? null : email,
        "gender": gender == null ? null : gender,
        "dateOfBirth": dateOfBirth == null ? null : dateOfBirth,
        "parentEmail": parentEmail == null ? null : parentEmail,
        "level": level,
        "numQuizzes": numQuizzes,
        "numBooks": numBooks,
        "lastLoggedIn": lastLoggedIn,
        "settings": settings != null ? settings?.toJson() : null,
        "totalLoginTime": totalLoginTime,
        "totalGameTime": totalGameTime,
        "timestampCreated": timestampCreated,
        "excludeFromLeaderboard": excludeFromLeaderboard,
        "profileImageUrl": profileImageUrl
      };
}
